#ifndef CLIB_CONFIG_H
#define CLIB_CONFIG_H

#pragma once

#include "clib_type.h"

#ifdef __cplusplus
extern "C" {
#endif

#define CLIB_CONFIG_LINE_MAX    1024
#define CLIB_CONFIG_FILED_MAX   64
#define CLIB_CONFIG_DEPTH       64

#define clib_config_malloc      malloc
#define clib_config_free        free

enum clib_config_type
{
    clib_config_type_none,
    clib_config_type_u32,
    clib_config_type_i32,
    clib_config_type_u64,
    clib_config_type_i64,
    clib_config_type_d64,
    clib_config_type_str,
    clib_config_type_obj,
    clib_config_type_list,
};

typedef struct clib_config_s clib_config_t;

typedef union clib_config_value
{
    u32_t           u32;
    i32_t           i32;
    u64_t           u64;
    i64_t           i64;
    double          d64;
    void           *ptr;
    clib_config_t  *obj;
} clib_config_value_t;

struct clib_config_s {
    i08_t                   key[CLIB_CONFIG_FILED_MAX]; 
    i08_t                   data[CLIB_CONFIG_FILED_MAX];
    u08_t                  *desc;    
    enum clib_config_type   type;
    union clib_config_value value;
    clib_config_t          *next;
};


//typedef struct clib_config_node_s clib_config_node_t;

void
clib_config_init(clib_config_t* cfg);

i32_t
clib_config_read(clib_config_t* cfg, const char* path);

clib_config_t*
clib_config_find_child(clib_config_t *node,const char* name);

clib_config_t*
clib_config_first_child(clib_config_t *node);

clib_config_t*
clib_config_item_next(clib_config_t *item);

i32_t
clib_config_count_item_by_name(clib_config_t *node,const char* name);

clib_config_t*
clib_config_get_item_by_index(clib_config_t *node,const char* name,i32_t index);

i32_t
clib_config_get_item_by_name(clib_config_t *node, clib_config_t **elem, const char* name);

i32_t
clib_config_all_child(clib_config_t *node, clib_config_t **elem);

clib_config_t*
clib_config_first_child(clib_config_t *root);

i32_t
clib_config_add_child(clib_config_t* root,clib_config_t* node);

i32_t
clib_config_write(clib_config_t* cfg, const char* path);

void
clib_config_clean(clib_config_t* cfg);

void
clib_config_item_to_string(clib_config_t* item);

#ifdef __cplusplus
}
#endif

#endif